package com.bigflexlib.x.mx.binding
{
	
	
	import com.bigflexlib.core.dependency.DependencySpec;
	import com.bigflexlib.core.dependency.MXMLObjectWithDependenciesBase;
	
	import mx.binding.utils.BindingUtils;
	import mx.binding.utils.ChangeWatcher;



	/**
	 * 
	 * Analog to mx:Binding but evaluated and implemented 
	 * at runtime using mx.binding.BindingUtils under the hood
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class RuntimeBinding extends MXMLObjectWithDependenciesBase
	{
		
		
		
		private var siteSpec:DependencySpec = new DependencySpec( "site", "*" );
		[Bindable("siteChanged")]
		public function get site( ):Object
		{
			return dh.getDependencyValue( siteSpec );
		}		
		public function set site( v:Object ):void
		{
			dh.setDependencyValue( siteSpec, v );
		}
		
		

		private var propSpec:DependencySpec = new DependencySpec( "prop", "*" );
		[Bindable("propChanged")]
		public function get prop( ):String
		{
			return dh.getDependencyValue( propSpec );
		}		
		public function set prop( v:String ):void
		{
			dh.setDependencyValue( propSpec, v );
		}	
		
		
		
		
		private var hostSpec:DependencySpec = new DependencySpec( "host", "*" );
		[Bindable("hostChanged")]
		public function get host( ):Object
		{
			return dh.getDependencyValue( hostSpec );
		}		
		public function set host( v:Object ):void
		{
			dh.setDependencyValue( hostSpec, v );
		}		
		
		

		private var chainSpec:DependencySpec = new DependencySpec( "chain", "*" );
		[Bindable("chainChanged")]
		public function get chain( ):Object
		{
			return dh.getDependencyValue( chainSpec );
		}		
		public function set chain( v:Object ):void
		{
			dh.setDependencyValue( chainSpec, v );
		}			
		
		

		private var setterSpec:DependencySpec = new DependencySpec( "setter", "*" );
		[Bindable("setterChanged")]
		public function get setter( ):Function
		{
			return dh.getDependencyValue( setterSpec );
		}		
		public function set setter( v:Function ):void
		{
			dh.setDependencyValue( setterSpec, v );
		}			
		
		
		
		private var commitOnlySpec:DependencySpec = new DependencySpec( "commitOnly", "*", null, false, false );
		[Bindable("commitOnlyChanged")]
		public function get commitOnly( ):Boolean
		{
			return dh.getDependencyValue( commitOnlySpec );
		}
		public function set commitOnly( v:Boolean ):void
		{
			dh.setDependencyValue( commitOnlySpec, v );
		}		
		
		
		
		
		
		private var cw:ChangeWatcher;
		
		
		
		
		
		
		override protected function dependenciesUpdated( ):void
		{
			if ( cw )
			{
				cw.unwatch( );
				cw = null;
			}
			
			
			if ( !host || !chain ) // we don't have the minimum required properties
			{
				return;
			}
				
			if ( setter is Function )
			{
				cw = BindingUtils.bindSetter( setter, host, chain, commitOnly );		
			}
			else if ( site && prop )
			{
				cw = BindingUtils.bindProperty( site, prop, host, chain, commitOnly );			
			}
									
			
		}
		
	}

}








